<template>
  <div class="ma-content-block lg:flex justify-between">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <template #tableSearch>
        <a-col :span="6">
          <a-form-item field="goods" label="ID|名称">
            <a-input allow-clear v-model="searchForm.goods" placeholder="请输入" @keydown.enter="refresh" />
          </a-form-item>
        </a-col>
      </template>
      <template #state="{ record }">
        <a-tag color="green" v-if="record.state == 1">{{ record.state_text }}</a-tag>
        <a-tag color="magenta" v-else>{{ record.state_text }}</a-tag>
      </template>
    </sa-table>
    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive, computed } from 'vue'
import mallApi from '@/api/system/mall'
import SaTable from '@cps/sa-table/index.vue'
import { Message } from '@arco-design/web-vue'
import { throttle } from 'lodash';
import EditForm from './edit.vue'
const crudRef = ref()
const editRef = ref()
const searchForm = ref({
  goods:''
})
const deleteForm = ref({ //刪除 确认彈窗
  id: '',
  pay_password: '',
})
const options = reactive({
  api: mallApi.skuList,//表格数据接口
  operationColumnWidth: 120,//操作欄
  showSearch: true,
  columnAlign: 'center',
  pageSimple: false,//是否展示操作欄有 以及工具欄
  isTreeTable: false,
  add: {//頂部新增按鈕
    show: true,
    auth: ['/core/menu/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {//操作欄编辑按鈕
    show: true,
    auth: ['/core/menu/update'],
    func: async (record) => {
      editRef.value?.open('edit', record.id)
      editRef.value?.setFormData(record)
    },
  },
  delete: {//操作欄和頂部刪除按鈕
    show: false,
    auth: ['/core/menu/destroy'],
    func: throttle(async (params) => {
      deleteForm.value.id = params.ids[0]
      const { data } = await infoApi.delInfo(deleteForm.value)
      Message.success(`刪除成功`)
      refresh()
    }, 2000)
  },
  isExpand: false,//展開所有子樹
})
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 100 },
  { title: 'SKU', dataIndex: 'sku', width: 150 },
  { title: '型号', dataIndex: 'model', width: 150 },
  { title: 'sku类型', dataIndex: 'type_text', width: 100 },
  { title: '在库总数', dataIndex: 'stock', width: 100 },
  { title: '库存预警', dataIndex: 'num_alert', width: 100 },
  { title: '状态', dataIndex: 'state', width: 100 },
  { title: '中文名称', dataIndex: 'name', width: 150 },
  { title: '英文名称', dataIndex: 'name_en', width: 150 },
  { title: '产品类型', dataIndex: 'goods_type_text', width: 100 },
  { title: '海关编码', dataIndex: 'customs_code', width: 100 },
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
])

const initPage = async () => {

}
const refresh = async () => {
  crudRef.value?.refresh()
}
onMounted(async () => {
  initPage()
  refresh()
})
</script>

<style scoped>
.icon {
  width: 1em;
}
</style>